package com.prestamomo.vil.ppa.pmvilpage.pmfitter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilGoods;

import java.util.List;

public class PmvilGoodsFiter extends RecyclerView.Adapter<PmvilGoodsFiter.PmvilGodsHolde> {

    private Context pmvilGdsCont;
    private List<PmvilGoods.LoanOrderRecordsBean> pmvilGdsList;

    public PmvilGoodsFiter(Context pmvilGdsCont, List<PmvilGoods.LoanOrderRecordsBean> pmvilGdsList) {
        this.pmvilGdsCont = pmvilGdsCont;
        this.pmvilGdsList = pmvilGdsList;
    }

    @NonNull
    @Override
    public PmvilGoodsFiter.PmvilGodsHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PmvilGodsHolde(LayoutInflater.from(pmvilGdsCont).inflate(R.layout.goods_pmvil_item_ly, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PmvilGoodsFiter.PmvilGodsHolde holder, int position) {
        Glide.with(pmvilGdsCont).load(pmvilGdsList.get(position).getIcon()).placeholder(R.mipmap.logo_pm_icon).into(holder.gdsPmvilIvicon);
        holder.gdsPmvilTvnam.setText(pmvilGdsList.get(position).getProductName());
        if (pmvilGdsList.get(position).getApplyStatus().equals("8")) {
            holder.gdsPmvilLlhk.setVisibility(View.GONE);
            holder.gdsPmvilIvwc.setVisibility(View.VISIBLE);
            holder.gdsPmvilRlmny.setVisibility(View.VISIBLE);
            holder.gdsPmvilTvmnyms.setText("支付金額");
            holder.gdsPmvilTvmny.setText("$"+pmvilGdsList.get(position).getApplyAmount());
            holder.gdsPmvilTvtimems.setText("付款時間");
            holder.gdsPmvilTvtime.setText(pmvilGdsList.get(position).getRepayDate());
            holder.gdsPmvilTvtime.setTextColor(Color.parseColor("#FF999999"));
            holder.gdsPmvilTvyq.setVisibility(View.GONE);
        } else if (pmvilGdsList.get(position).getApplyStatus().equals("2")
                || pmvilGdsList.get(position).getApplyStatus().equals("4")
                || pmvilGdsList.get(position).getApplyStatus().equals("10")) {
            holder.gdsPmvilLlhk.setVisibility(View.GONE);
            holder.gdsPmvilIvwc.setVisibility(View.GONE);
            holder.gdsPmvilRlmny.setVisibility(View.GONE);
            holder.gdsPmvilTvtimems.setText("申請時間");
            holder.gdsPmvilTvtime.setText(pmvilGdsList.get(position).getRepayDate());
            holder.gdsPmvilTvtime.setTextColor(Color.parseColor("#FF999999"));
            holder.gdsPmvilTvyq.setVisibility(View.GONE);
        }else {
            if (pmvilGdsList.get(position).getApplyStatus().equals("9")) {
                holder.gdsPmvilLlhk.setVisibility(View.VISIBLE);
                holder.gdsPmvilIvwc.setVisibility(View.GONE);
                holder.gdsPmvilRlmny.setVisibility(View.VISIBLE);
                holder.gdsPmvilTvmnyms.setText("貸款額度");
                holder.gdsPmvilTvmny.setText("$"+pmvilGdsList.get(position).getApplyAmount());
                holder.gdsPmvilTvtimems.setText("最後期限");
                holder.gdsPmvilTvtime.setText(pmvilGdsList.get(position).getRepayDate());
                holder.gdsPmvilTvtime.setTextColor(Color.parseColor("#E30000"));
                holder.gdsPmvilTvyq.setVisibility(View.VISIBLE);
            }else if (pmvilGdsList.get(position).getApplyStatus().equals("7")) {
                holder.gdsPmvilLlhk.setVisibility(View.VISIBLE);
                holder.gdsPmvilIvwc.setVisibility(View.GONE);
                holder.gdsPmvilRlmny.setVisibility(View.VISIBLE);
                holder.gdsPmvilTvmnyms.setText("貸款額度");
                holder.gdsPmvilTvmny.setText("$"+pmvilGdsList.get(position).getApplyAmount());
                holder.gdsPmvilTvtimems.setText("最後期限");
                holder.gdsPmvilTvtime.setText(pmvilGdsList.get(position).getRepayDate());
                holder.gdsPmvilTvtime.setTextColor(Color.parseColor("#333333"));
                holder.gdsPmvilTvyq.setVisibility(View.GONE);
            }else {
                holder.gdsPmvilLlhk.setVisibility(View.GONE);
                holder.gdsPmvilIvwc.setVisibility(View.GONE);
                holder.gdsPmvilRlmny.setVisibility(View.VISIBLE);
                holder.gdsPmvilTvmnyms.setText("貸款額度");
                holder.gdsPmvilTvmny.setText("$"+pmvilGdsList.get(position).getApplyAmount());
                holder.gdsPmvilTvtimems.setText("申請日期");
                holder.gdsPmvilTvtime.setText(pmvilGdsList.get(position).getApplyTime());
                holder.gdsPmvilTvtime.setTextColor(Color.parseColor("#333333"));
                holder.gdsPmvilTvyq.setVisibility(View.GONE);
            }
        }

        holder.gdsPmvilLlhk.setOnClickListener(v -> {
            if(pmvilGdsClick!=null){
                pmvilGdsClick.setPmvilGdsProduct(pmvilGdsList.get(position));
            }
        });

    }

    @Override
    public int getItemCount() {
        return pmvilGdsList.size();
    }

    public class PmvilGodsHolde extends RecyclerView.ViewHolder {

        private  ImageView gdsPmvilIvicon;
        private  TextView gdsPmvilTvnam;
        private  LinearLayout gdsPmvilLlhk;
        private  ImageView gdsPmvilIvwc;
        private  RelativeLayout gdsPmvilRlmny;
        private  TextView gdsPmvilTvmnyms;
        private  TextView gdsPmvilTvmny;
        private  TextView gdsPmvilTvtimems;
        private  TextView gdsPmvilTvyq;
        private  TextView gdsPmvilTvtime;

        public PmvilGodsHolde(@NonNull View itemView) {
            super(itemView);
            initPmvilGoods(itemView, PmvilApp.getPmvilApss());
            gdsPmvilIvwc = itemView.findViewById(R.id.gds_pmvil_iv_wc);
            gdsPmvilRlmny = itemView.findViewById(R.id.gds_pmvil_rl_mny);
            gdsPmvilTvmnyms = itemView.findViewById(R.id.gds_pmvil_tv_mny_ms);
            initPmvilGodsliw(itemView);
        }

        private void initPmvilGodsliw(@NonNull View itemView) {
            gdsPmvilTvmny = itemView.findViewById(R.id.gds_pmvil_tv_mny);
            gdsPmvilTvtimems = itemView.findViewById(R.id.gds_pmvil_tv_time_ms);
            gdsPmvilTvyq = itemView.findViewById(R.id.gds_pmvil_tv_yq);
            gdsPmvilTvtime = itemView.findViewById(R.id.gds_pmvil_tv_time);
        }

        private void initPmvilGoods(@NonNull View itemView,Context pmvilCOt) {
            gdsPmvilIvicon = itemView.findViewById(R.id.gds_pmvil_iv_icon);
            gdsPmvilTvnam = itemView.findViewById(R.id.gds_pmvil_tv_nam);
            gdsPmvilLlhk = itemView.findViewById(R.id.gds_pmvil_ll_hk);

        }
    }

    public interface PmvilGdsClick{
        void setPmvilGdsProduct(PmvilGoods.LoanOrderRecordsBean pmvilGdsProduct);
    }

    private PmvilGdsClick pmvilGdsClick;

    public void setPmvilGdsClick(PmvilGdsClick pmvilGdsClick) {
        this.pmvilGdsClick = pmvilGdsClick;
    }
}
